<?php

class GlobalMethods {

    public static function obtenerDV($rut) {
        $s = 1;
        for ($m = 0; $rut != 0; $rut/=10

            )$s = ($s + $rut % 10 * (9 - $m++ % 6)) % 11;
        $dv = chr($s ? $s + 47 : 75);
        return $dv;
    }

    public static function validarRut($rut, $dv) {
        $digito = SubsidiosMunicipalidad::obtenerDV($rut);
        return (strcasecmp($digito, $dv) == 0);
    }

    public static function formatoFecha($str) {
        if (!$str) {
            return "";
        }
        $array_fecha = explode(' ', $str);
        $date = $array_fecha[0];

        // si solamente viene el día, pero no la hora, entonces este if evita el error
        if (count($array_fecha) > 1) {
            $time = $array_fecha[1];
        } else {
            $time = false;
        }
        list($year, $month, $day) = explode('-', $date);
        if ($day > 1000)
            list($day, $month, $year) = explode('-', $date);
        if ($time) {
            list($hour, $minute, $second) = explode(':', $time);
        } else {
            $hour = 0;
            $minute = 0;
            $second = 0;
        }
        $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
        $fecha = getdate($timestamp);
        $vect_dia = array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado");
        $vect_mes = array("empty", "Ene.", "Feb.", "Mar.", "Abr.", "May.", "Jun.", "Jul.", "Ago.", "Sept.", "Oct.", "Nov.", "Dic.");
        $fecha_traduc = $fecha['mday'] . " " . $vect_mes[$fecha['mon']] . " " . $fecha['year'];
        return ($fecha_traduc);
    }

    public static function chart($type, $data, $leyend, $size) {
        $data_chart = implode(',', $data);
        $size_chart = implode('x', $size);
        $leyend_chart = implode('|', $leyend);
        $chart = 'https://chart.googleapis.com/chart?chs=' . $size_chart . '&chd=t:' . $data_chart . '&cht=' . $type . '&chdl=' . $leyend_chart;
        return($chart);
    }

    public static function setFormatoFechas($fecha) {
        $fecha;
        $array_fecha = explode(" ", $fecha);
        $array_fecha = explode("-", $array_fecha[0]);

        return $array_fecha;
    }

    public static function FormatearNumero($numero) {
        $numero = $numero;

        $n = strlen($numero) - 1;
        $numero_formateado = '';
        $contador = 0;

        while ($n > -1) {
            if ($contador % 3 == 0 && $contador != 0 && $n < (strlen($numero))) {
                $numero_formateado = $numero[$n] . '.' . $numero_formateado;
            } else {
                $numero_formateado = $numero[$n] . $numero_formateado;
            }
            $contador++;
            $n--;
        }
        return $numero_formateado;
    }

    public static function sacarIdMaximo($tabla) {
        $connection = Doctrine_Manager::connection();
        $query = 'SELECT MAX(id) AS max FROM ' . $tabla;
        $statement = $connection->execute($query);
        $statement->execute();
        $resultset = $statement->fetch(PDO::FETCH_OBJ);
        $max = $resultset->max;
        return $max;
    }

  
}

